package com.woniu.deal.order.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.DealInsertOrderDTO;
import com.woniu.common.dto.DealOrderDTO;
import com.woniu.common.dto.WaresDTO;
import com.woniu.common.entity.DealOrder;
import com.woniu.common.entity.DealRechargeorder;
import com.woniu.common.entity.Result;
import com.woniu.deal.order.feignclient.ElderlyWaresFeign;
import com.woniu.deal.order.mapper.DealRechargeorderMapper;
import com.woniu.deal.order.service.DealOrderService;
import com.woniu.deal.order.mapper.DealOrderMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【deal_order】的数据库操作Service实现
 * @createDate 2024-08-02 11:03:09
 */
@Service
public class DealOrderServiceImpl extends ServiceImpl<DealOrderMapper, DealOrder>
        implements DealOrderService {

    @Autowired
    DealOrderMapper dealOrderMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    DealRechargeorderMapper dealRechargeorderMapper;
    @Autowired
    ElderlyWaresFeign elderlyWaresFeign;

    @Override
    public List<DealOrder> getListByConditionPage(int pageIndex, int pageSize, DealOrderDTO dealOrderDTO) {
        Wrapper<DealOrder> wrapper = new LambdaQueryWrapper<>(DealOrder.class)
                //服务类别
                .eq(dealOrderDTO.getCategory_id() != null, DealOrder::getServerId, dealOrderDTO.getCategory_id())
                //下单时间
                .between(dealOrderDTO.getOrderStartDate() != null && dealOrderDTO.getOrderEndDate() != null,
                        DealOrder::getOrderPlacedate,
                        dealOrderDTO.getOrderStartDate(),
                        dealOrderDTO.getOrderEndDate())
                //实付金额
                .between(dealOrderDTO.getLeastAmount() != null && dealOrderDTO.getMostAmount() != null,
                        DealOrder::getOrderAmout, dealOrderDTO.getLeastAmount(), dealOrderDTO.getMostAmount());
        //关键字模糊查询(暂时不好实现)
        //.like(!StringUtils.isEmpty(dealOrderDTO.getKeyWord()),Deal)
        Page<DealOrder> page = baseMapper.selectPage(new Page<>(pageIndex, pageSize), wrapper);
        return page.getRecords();
    }

    @Override
    public synchronized String addOrder(HttpServletRequest request, DealInsertOrderDTO dealInsertOrderDTO) {
        //dealInsertOrderDTO是前端传过来的数据
        //调用老张的接口获取商品信息
        Result<WaresDTO> waresDTO = elderlyWaresFeign.getWaresSku(dealInsertOrderDTO.getSkuId());
        dealInsertOrderDTO.setWaresDTO(waresDTO.getData());
        DealOrder dealOrder = new DealOrder();
        String num = IdUtil.getSnowflake().nextIdStr();
        dealOrder.setOrderId(null);
        //userID等冉雄写好token 从token中获取
        dealOrder.setUserId(1/* 本来应该这么写，但是还没和老冉联动起来，所以暂时搁置 Integer.valueOf(request.getHeader("userId"))*/);
        dealOrder.setSkuId(dealInsertOrderDTO.getWaresDTO().getSkuId());
        dealOrder.setCategoryId(dealInsertOrderDTO.getWaresDTO().getCategoryCid());
        dealOrder.setOrderNum(num);
        dealOrder.setOrderName(dealInsertOrderDTO.getWaresDTO().getSpuName());
        //实付款等于商品价格减去优惠券，判断有无优惠券，没有优惠券的话就按原价处理，测试阶段直接按原价处理
        //本来应该根据用户id判断redis中是否有优惠券，暂未开发，暂时搁置
        dealOrder.setOrderPrice(dealInsertOrderDTO.getWaresDTO().getPrice());
        dealOrder.setOrderAmout(dealInsertOrderDTO.getWaresDTO().getPrice());
        dealOrder.setOrderStatus("未支付");
        dealOrder.setOrderPlacedate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        //订单来源做判断，通过请求方式判断订单来源，测试阶段都设置为APP
        dealOrder.setOrderSource("APP");
        rabbitTemplate.convertAndSend("exchangeinsertorder", "insertorder", dealOrder);
        return num;
    }

    @Override
    public DealOrder getOneDealOrderById(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public DealRechargeorder selectByOrderNum(String dealOrderNum) {
        return dealRechargeorderMapper.selectByOrderNum(dealOrderNum);
    }

    @Override
    public List<DealOrder> getOneAllOrders(int userId) {
        List<DealOrder> dealOrders = dealOrderMapper.findByUserId(userId);
        System.out.println(dealOrders);
        return dealOrders;
    }

    @Override
    public void modify(DealOrder dealOrder) {
        System.out.println(dealOrder);
        System.out.println("1");
        baseMapper.updateById(dealOrder);
    }

}




